package com.study.webapp.lock;

import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockTest implements Runnable {

	private static ReentrantLock lock = new ReentrantLock();

	private static int i = 0;

	@Override
	public void run() {

		for (int j = 0; j < 1000; j++) {
			try {
				// 需要用就添加，更加灵活
				lock.lock();
				i++;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				// 不过一定要手动释放锁
				lock.unlock();
			}
		}

	}

	public static void main(String[] args) throws Exception {
		ReentrantLockTest t = new ReentrantLockTest();
		Thread t1 = new Thread(t);
		Thread t2 = new Thread(t);
		t1.start();
		t2.start();
		t1.join();
		t2.join();
		System.out.println(i);
	}

}
